package com.ruoyi.column.service.impl;

import java.util.List;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SnowflakeUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.column.mapper.SysColumnMapper;
import com.ruoyi.column.domain.SysColumn;
import com.ruoyi.column.service.ISysColumnService;

/**
 * 专栏Service业务层处理
 * 
 * @author yjf
 */
@Service
public class SysColumnServiceImpl implements ISysColumnService 
{
    @Autowired
    private SysColumnMapper sysColumnMapper;

    /**
     * 查询专栏
     * 
     * @param columnId 专栏主键
     * @return 专栏
     */
    @Override
    public SysColumn selectSysColumnByColumnId(Long columnId)
    {
        return sysColumnMapper.selectSysColumnByColumnId(columnId);
    }

    /**
     * 查询专栏列表
     * 
     * @param sysColumn 专栏
     * @return 专栏
     */
    @Override
    public List<SysColumn> selectSysColumnList(SysColumn sysColumn)
    {
        
        if (StringUtils.isNull(sysColumn.getDelFlag())) {
            sysColumn.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        return sysColumnMapper.selectSysColumnList(sysColumn);
    }

    /**
     * 新增专栏
     * 
     * @param sysColumn 专栏
     * @return 结果
     */
    @Override
    public int insertSysColumn(SysColumn sysColumn)
    {
        //发布时间
        if(SysConstants.CONSTANT_STR1.equals(sysColumn.getStatus())){
            sysColumn.setReleaseTime(DateUtils.getNowDate());
        }
        //编号
        sysColumn.setColumnNo(SnowflakeUtils.snowflakeId());
        //创建者ID
        sysColumn.setUserId(SecurityUtils.getUserId());
        sysColumn.setCreateTime(DateUtils.getNowDate());
        sysColumn.setUpdateTime(DateUtils.getNowDate());
        return sysColumnMapper.insertSysColumn(sysColumn);
    }

    /**
     * 修改专栏
     * 
     * @param sysColumn 专栏
     * @return 结果
     */
    @Override
    public int updateSysColumn(SysColumn sysColumn)
    {
        //发布时间
        SysColumn info = sysColumnMapper.selectSysColumnByColumnId(sysColumn.getColumnId());
        if(StringUtils.isNotNull(info) && info.getStatus() != sysColumn.getStatus())
        if(SysConstants.CONSTANT_STR1.equals(sysColumn.getStatus().toString())){
            sysColumn.setReleaseTime(DateUtils.getNowDate());
        }
        sysColumn.setUpdateTime(DateUtils.getNowDate());
        return sysColumnMapper.updateSysColumn(sysColumn);
    }

    /**
     * 批量删除专栏
     * 
     * @param columnIds 需要删除的专栏主键
     * @return 结果
     */
    @Override
    public int deleteSysColumnByColumnIds(Long[] columnIds)
    {
        return sysColumnMapper.deleteSysColumnByColumnIds(columnIds);
    }

    /**
     * 删除专栏信息
     * 
     * @param columnId 专栏主键
     * @return 结果
     */
    @Override
    public int deleteSysColumnByColumnId(Long columnId)
    {
        return sysColumnMapper.deleteSysColumnByColumnId(columnId);
    }

    /**
     * 校验专栏名称是否唯一
     * @param sysColumn 专栏
     * @return 结果
     */
    @Override
    public String checkColumnNameUnique(SysColumn sysColumn)
    {
        sysColumn.setDelFlag(SysConstants.CONSTANT_STR0);
        int count = sysColumnMapper.checkColumnNameUnique(sysColumn);
        if (count > 0)
        {
            return SysConstants.CONSTANT_STR1;
        }
        return SysConstants.CONSTANT_STR0;
    }
}
